# -*- coding: utf-8 -*-

from autoscrapy.db import article
from autoscrapy.utils import log


LOG = log.get_logging(__name__)

class item(object):

    def __init__(self, context):
        self._context = context

    @property
    def session(self):
        return self._context['session']

    def add_item(self, content):
        if not content:
            return

        item = article.article(
            title=content['title'].encode('utf-8')[:98] if content['title'] else '',
            content=content['content'].encode('utf-8')[:15000] if content['content'] else '',
            images=','.join(content['images']).encode('utf-8')[:98],
            fromsrc=content['fromsrc'].encode('utf-8')[:98] if content['fromsrc'] else ''
        )
        try:
            self.session.add(item)
        except:
            LOG.error("add item %s fail", content)

